<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:p="http://primefaces.org/ui"
      xmlns:f="http://java.sun.com/jsf/core">

    <ui:composition template="layout.xhtml">
        <ui:define name="body">

            <h:form id="registrationForm" styleClass="formAverbacao" enctype="multipart/form-data">
                <br/>
                <div style="float: left;width: 100%">
                    <h3 class="title" style="float: left">Averbação de Transferência</h3>
                </div>

                <ui:include src="averbacaocabecalho.xhtml"/>  
                <ui:include src="averbacaorodape.xhtml"/>  

                <div style="float: left;width: 100%;margin-top: 40px">
                    <h3 class="title" style="float: left;padding-left: 20px">Proprietários</h3>
                </div>
                <p:growl id="messages"  autoUpdate="true"  /> 

                <p:panel id="panelOld" style="background: none; border: none;" 
                         styleClass="container_24 clearfix ">

                    <p:dataTable id="tableProprietarioOld" var="item" value="#{managerAverbacao.atuaisProprietarios}" 
                                 styleClass="grid_24" emptyMessage="Nenhum registro">  
                        <f:facet name="header">
                            Proprietários Atuais
                        </f:facet>

                        <p:column headerText="Ação"  >
                            <p:commandButton icon="ui-icon-arrowthick-1-s" title="Copiar" process="@this" actionListener="#{managerAverbacao.selecionaProprietario(item)}" update="tableProprietarioOld, :registrationForm:tableProprietario" />
                        </p:column>

                        <p:column headerText="Nome">  
                            #{item.tipoDocumento ne null ? 
                              (item.tipoDocumento eq 'CPF' ? item.nome : item.nomeFantasia) : 
                              (item.nome ne null and item.nome ne '' ? item.nome : item.nomeFantasia)}
                        </p:column>

                        <p:column headerText="Documento" >  
                            #{item.formatedDocument()}
                        </p:column> 

                    </p:dataTable> 
                </p:panel>

                <div style="float: left;width: 100%;margin-bottom: 10px">
                    <p:commandButton value="Adicionar Proprietário" title="Adicionar Proprietário" 
                                     icon="ui-icon-plus" process="@this" 
                                     style="float: right;margin-right: 15px" 
                                     onclick="dlgProprietario.show()" />
                </div>

                <p:panel id="panel" style="background: none; border: none;" 
                         styleClass="container_24 clearfix ">

                    <p:dataTable id="tableProprietario" var="item" value="#{managerAverbacao.novosProprietarios}" 
                                 styleClass="grid_24" emptyMessage="Nenhum registro">  
                        <f:facet name="header">
                            Novos Proprietários
                        </f:facet>

                        <p:column headerText="Ação"  >
                            <p:commandButton icon="ui-icon-trash" title="Remover" 
                                             process="@this" update="tableProprietario" 
                                             actionListener="#{managerAverbacao.removeProprietario(item)}" />
                        </p:column>

                        <p:column headerText="Nome">  
                            #{item.tipoDocumento ne null ? 
                              (item.tipoDocumento eq 'CPF' ? item.nome : item.nomeFantasia) : 
                              (item.nome ne null and item.nome ne '' ? item.nome : item.nomeFantasia)}
                        </p:column>

                        <p:column headerText="Documento" >  
                            #{item.formatedDocument()}
                        </p:column> 

                    </p:dataTable>

                    <div class="clear"/><br/>

                    <div class="buttonAction"> 
                        <p:commandButton value="Salvar" icon="ui-icon-disk" styleClass="ui-priority-primary"
                                         actionListener="#{managerAverbacao.salvarPorTransferencia()}"
                                         update="@form" process="@form"/>

                        <p:commandButton actionListener="#{managerAverbacao.redirectAverbacao()}" process="@this" value="Cancelar"  icon="ui-icon-trash" />
                    </div >
                    <p:dialog id="dlgPrint" width="450" modal="true" widgetVar="printDialog" header="Imprimir" closable="false" >
                        <p:outputLabel value="Deseja imprimir esta averbação ?" />
                        <div class="clear"/>

                        <div class="buttonAction" style="padding-top: 25px">
                            <p:commandButton title="Imprimir"
                                             styleClass="printButton"
                                             value="Sim" 
                                             ajax="false" update="@form"
                                             onclick="this.form.target='_blank'"
                                             actionListener="#{managerCriarImovel.imprimirAverbacao(managerAverbacao.averbacao)}"
                                             icon="ui-icon-print" />
                            <p:commandButton value="Não" icon="ui-icon-close" actionListener="#{managerAverbacao.redirect()}" />
                        </div>
                    </p:dialog>
                    <script>
                        $('.printButton').mouseup(function(){     
                            window.open("pesquisarimovel.xhtml", "_SELF");
                        });
                    </script>

                </p:panel>
                <p:panel>
                    <p:dialog widgetVar="dlgProprietario" id="dlgProprietario" closable="false"
                              modal="true" header="Novo Proprietário" height="500" 
                              width="1024" closeOnEscape="true">
                        <p:tabView id="tabviewProprietarios">
                            <p:tab title="Pesquisar">

                                <div style="margin-left: 56px">
                                    <p:outputLabel id="personOutputLabel" value="Pessoa" styleClass="grid_3" style="top: 6px" />
                                    <div class="grid_8">
                                        <p:selectOneRadio id="personRadioButton" 
                                                          value="#{managerProprietario.pesquisarProprietario.tipoDocumento}" >
                                            <f:selectItems value="#{managerProprietario.tipoDocumentoList()}" />
                                            <p:ajax event="change" update=":registrationForm:tabviewProprietarios:proprietarioPanel" />
                                        </p:selectOneRadio>
                                    </div>
                                </div>

                                <p:panel id="proprietarioPanel" 
                                         style="background: none; border: none; margin-left: -7px; margin-top: -20px;" 
                                         styleClass="container_24 clearfix both " >

                                    <div class="clear"/>

                                    <p:outputLabel id="tipoDocumentoLabel" 
                                                   value="#{managerProprietario.pesquisarProprietario.tipoDocumento}: " 
                                                   styleClass="grid_3"/>
                                    <f:validateBean disabled="true" >
                                        <p:inputMask id="tipoDocumentoInput" 
                                                     value="#{managerProprietario.pesquisarProprietario.tipoDocumento.tipo eq 'CPF' ? 
                                                              managerProprietario.pesquisarProprietario.cpf : managerProprietario.pesquisarProprietario.cnpj}"
                                                     mask="#{managerProprietario.pesquisarProprietario.tipoDocumento.tipo eq 'CPF' ? 
                                                             '999.999.999-99':'99.999.999/9999-99'}"
                                                     styleClass="grid_8" > 
                                        </p:inputMask>
                                    </f:validateBean>

                                    <div class="clear"/>
                                    <p:outputLabel value="Nome" styleClass="grid_3" for="searchNomeInputText" 
                                                   rendered="#{managerProprietario.pesquisarProprietario.tipoDocumento.tipo eq 'CPF'}"/>
                                    <p:inputText id="searchNomeInputText" styleClass="grid_20" 
                                                 value="#{managerProprietario.pesquisarProprietario.nome}" 
                                                 rendered="#{managerProprietario.pesquisarProprietario.tipoDocumento.tipo eq 'CPF'}" />

                                    <p:outputLabel value="Nome Fantasia" styleClass="grid_3 companyLabel" for="razaoSocialInputText"  
                                                   style="margin-left: -5px !important" 
                                                   rendered="#{managerProprietario.pesquisarProprietario.tipoDocumento.tipo eq 'CNPJ'}" />
                                    <p:inputText id="razaoSocialInputText" styleClass="grid_20"
                                                 value="#{managerProprietario.pesquisarProprietario.nomeFantasia}" 
                                                 rendered="#{managerProprietario.pesquisarProprietario.tipoDocumento.tipo eq 'CNPJ'}" />

                                </p:panel>
                                <br/>
                                <div class="buttonAction">
                                    <p:commandButton value="Pesquisar" icon="ui-icon-search"
                                                     update=":registrationForm:tabviewProprietarios:resultTable" id="addButton"
                                                     styleClass="ui-priority-primary" process=":registrationForm:tabviewProprietarios"
                                                     actionListener="#{managerProprietario.pesquisar()}" /> 

                                    <p:commandButton value="Limpar" icon="ui-icon-clean" process=":registrationForm:tabviewProprietarios"
                                                     update=":registrationForm:tabviewProprietarios:proprietarioPanel" 
                                                     actionListener="#{managerProprietario.limpar()}" />
                                </div >
                                <br />

                                <p:blockUI id="clockUi" block="resultPanel" trigger="addButton" > 
                                    <p:graphicImage value="media/images/ajax-loader.gif"/>
                                </p:blockUI>

                                <h4 class="title">Resultado</h4>
                                <p:panel id="resultPanel" style="background: none; border: none;" 
                                         styleClass="container_24 clearfix both ">

                                    <p:dataTable id="resultTable" var="cht" value="#{managerProprietario.dataModel}"
                                                 styleClass="grid_24" rowIndexVar="rowid"
                                                 binding="#{managerProprietario.searchResults}"
                                                 rows="5" paginator="true" lazy="true" rowKey="#{cht.id}"
                                                 paginatorTemplate="{FirstPageLink} {PreviousPageLink} {CurrentPageReport} {NextPageLink} {LastPageLink}"
                                                 currentPageReportTemplate="{currentPage} de {totalPages}"
                                                 paginatorPosition="bottom" emptyMessage="Nenhum registro encontrado">
                                        <f:facet name="header">
                                            Proprietários
                                        </f:facet> 
                                        <p:column headerText="Ação" style="width: 50px; vertical-align: middle;">  
                                            <p:commandButton actionListener="#{managerAverbacao.selecionaProprietario(cht)}" icon="ui-icon-check"
                                                             title="Selecionar" onsuccess="dlgProprietario.hide()" update=":registrationForm:tableProprietario" process=":registrationForm:tabviewProprietarios" />
                                        </p:column> 
                                        <p:column headerText="Nome / Fantasia" >  
                                            <h:outputLabel value="#{cht.tipoDocumento.tipo eq 'CPF' ? cht.nome : cht.nomeFantasia}" />  
                                        </p:column>
                                        <p:column headerText="Documento" >  
                                            <h:outputLabel value="#{cht.formatedDocument()}" />  
                                        </p:column>
                                    </p:dataTable>
                                </p:panel>

                                <div class="buttonAction">
                                    <p:commandButton process="@this" onclick="dlgProprietario.hide()" value="Fechar"  icon="ui-icon-circle-close" />
                                </div>
                            </p:tab>

                            <p:tab title="Cadastrar">

                                <p:panel id="personPanel" style="background: none; border: none;" 
                                         styleClass="container_24 clearfix both ">
                                    <h4 class="title">Dados Gerais</h4>

                                    <p:outputLabel id="AddpersonOutputLabel" value="Pessoa: " styleClass="grid_3" style="top: 6px" />
                                    <div class="grid_8">
                                        <p:selectOneRadio id="addPersonRadioButton" value="#{managerAverbacao.proprietario.tipoDocumento}" >
                                            <f:selectItems value="#{managerAverbacao.tipoDocumentoList()}" />
                                            <p:ajax event="change" update="personPanel" />
                                        </p:selectOneRadio>
                                    </div>

                                    <div class="clear-both"/>

                                    <p:outputLabel for="tipoDocumentoInput"
                                                   value="#{managerAverbacao.proprietario.tipoDocumento}: " 
                                                   styleClass="grid_3"/>

                                    <p:inputMask id="addTipoDocumentoInput"
                                                 value="#{managerAverbacao.proprietario.tipoDocumento.tipo eq 'CPF' ? 
                                                          managerAverbacao.proprietario.cpf : managerAverbacao.proprietario.cnpj}"
                                                 mask="#{managerAverbacao.proprietario.tipoDocumento.tipo eq 'CPF' ? 
                                                         '999.999.999-99':'99.999.999/9999-99'}"
                                                 styleClass="grid_8" > 
                                    </p:inputMask>
                                    <div class="clear-both" />

                                    <p:outputLabel value="Nome: " styleClass="grid_3" for="nomeInputText" 
                                                   rendered="#{managerAverbacao.proprietario.tipoDocumento.tipo eq 'CPF'}"/>
                                    <p:inputText id="nomeInputText" styleClass="grid_20 ajustInput"
                                                 value="#{managerAverbacao.proprietario.nome}" 
                                                 rendered="#{managerAverbacao.proprietario.tipoDocumento.tipo eq 'CPF'}" />

                                    <p:outputLabel value="RG: " styleClass="grid_3" for="rgInputText" 
                                                   rendered="#{managerAverbacao.proprietario.tipoDocumento.tipo eq 'CPF'}" />
                                    <p:inputText id="rgInputText" styleClass="grid_8" value="#{managerAverbacao.proprietario.rg}" 
                                                 rendered="#{managerAverbacao.proprietario.tipoDocumento.tipo eq 'CPF'}" />

                                    <p:outputLabel value="Org. Emissor: " styleClass="grid_3" for="orgInputText" 
                                                   rendered="#{managerAverbacao.proprietario.tipoDocumento.tipo eq 'CPF'}" />
                                    <p:inputText id="orgInputText" styleClass="grid_8" value="#{managerAverbacao.proprietario.orgaoEmissor}" 
                                                 rendered="#{managerAverbacao.proprietario.tipoDocumento.tipo eq 'CPF'}" />
                                    <div class="clear"/> 
                                    <p:outputLabel value="Data Nasc.: " styleClass="grid_3" for="dateNascCalendar" 
                                                   rendered="#{managerAverbacao.proprietario.tipoDocumento.tipo eq 'CPF'}" />
                                    <p:calendar id="dateNascCalendar" value="#{managerAverbacao.proprietario.dataNasc}" 
                                                styleClass="grid_8" 
                                                showOn="both" 
                                                locale="pt"
                                                yearRange="1900:2020"
                                                navigator="true"
                                                pattern="dd/MM/yyyy"
                                                rendered="#{managerAverbacao.proprietario.tipoDocumento.tipo eq 'CPF'}" />

                                    <div class="clear"/> 
                                    <p:outputLabel value="Nome Fantasia: " styleClass="grid_3 companyLabel" style="margin-left: -7px !important"
                                                   for="nameFantasyInputText" rendered="#{managerAverbacao.proprietario.tipoDocumento.tipo eq 'CNPJ'}" />
                                    <p:inputText id="nameFantasyInputText" styleClass="grid_20 ajustInput" 
                                                 value="#{managerAverbacao.proprietario.nomeFantasia}" 
                                                 rendered="#{managerAverbacao.proprietario.tipoDocumento.tipo eq 'CNPJ'}" />
                                    <div class="clear"/> 
                                    <br/>

                                    <p:panel id="addressPanel"
                                             style="background: none; border: none; margin-left: -15px; margin-top: -25px;" 
                                             styleClass="container_24 clearfix both ">
                                        <h4 class="title">Endereço</h4>

                                        <p:outputLabel value="CEP" styleClass="grid_3" for="cepInputMask" />
                                        <p:inputMask  id="cepInputMask" styleClass="grid_4" mask="99.999-999"
                                                      value="#{managerAverbacao.proprietario.endereco.cep}" > 
                                        </p:inputMask>
                                        <div class="clear"/>

                                        <p:outputLabel value="Cod. Logra." styleClass="grid_3" 
                                                       for="codLogradouroInputText" />
                                        <p:inputText id="codLogradouroInputText" styleClass="grid_4" 
                                                     value="#{managerAverbacao.proprietario.endereco.codLogradouro}" />

                                        <p:outputLabel value="Logradouro" styleClass="grid_4 alpha" for="patioInputText" />
                                        <p:inputText id="patioInputText" styleClass="grid_12" 
                                                     value="#{managerAverbacao.proprietario.endereco.logradouro}" />

                                        <p:outputLabel value="Cod. Bairro" styleClass="grid_3" for="codBairroInputText" />
                                        <p:inputText id="codBairroInputText" styleClass="grid_4" 
                                                     value="#{managerAverbacao.proprietario.endereco.codBairro}" />

                                        <p:outputLabel value="Bairro: " styleClass="grid_4 alpha" for="bairroInputText" />
                                        <p:inputText id="bairroInputText" styleClass="grid_12" 
                                                     value="#{managerAverbacao.proprietario.endereco.bairro}" />

                                        <p:outputLabel value="Estado: " styleClass="grid_3" for="estadoInputText" />
                                        <p:inputText id="estadoInputText" styleClass="grid_4"
                                                     value="#{managerAverbacao.proprietario.endereco.uf}" />

                                        <p:outputLabel value="Município: " styleClass="grid_4 alpha" for="municipioInputText" />
                                        <p:inputText id="municipioInputText" styleClass="grid_12"
                                                     value="#{managerAverbacao.proprietario.endereco.municipio}" />

                                        <p:outputLabel value="Número: " styleClass="grid_3" for="numInputText" />
                                        <p:inputText id="numInputText" styleClass="grid_4"
                                                     value="#{managerAverbacao.proprietario.endereco.numero}" />

                                        <p:outputLabel value="Complemento" styleClass="grid_4 alpha" for="complementInputText" />
                                        <p:inputText id="complementInputText" styleClass="grid_12" 
                                                     value="#{managerAverbacao.proprietario.endereco.compl}" />

                                    </p:panel>
                                </p:panel>

                                    
                            
                                <div class="buttonAction">
                                    <p:commandButton value="Adicionar" icon="ui-icon-circle-plus"
                                                     actionListener="#{managerAverbacao.addProprietario()}"
                                                     oncomplete="dlgProprietario.hide()"
                                                     process=":registrationForm:dlgProprietario"
                                                     update=":registrationForm:tableProprietario, :registrationForm:dlgProprietario"
                                                     styleClass="ui-priority-primary" />

                                    <p:commandButton process="@this" onclick="dlgProprietario.hide()" value="Cancelar"  icon="ui-icon-trash" />
                                </div >
                            </p:tab>
                        </p:tabView>

                    </p:dialog>
                </p:panel>

                <br/>
            </h:form>
        </ui:define>
    </ui:composition>

</html>
